package com.ruoyi.yuce.controller;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.yuce.domain.FaultRecord;
import com.ruoyi.yuce.domain.YcData;
import com.ruoyi.yuce.service.IChangzhanService;
import com.ruoyi.yuce.service.IFengjiService;
import com.ruoyi.yuce.service.IJYTZService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ruoyi-yuce/yxzk")
@DataSource(value = DataSourceType.JYTZ)
public class YxzkController extends BaseController {

    @Autowired
    private IChangzhanService changzhanService;

    @Autowired
    private IFengjiService fengjiService;

    @Autowired
    private IJYTZService jytzService;

    /**
     * 获取场站数目
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getChangzhanNum')")
    @GetMapping("/getChangzhanNum")
    public int getChangzhanNum(){
        logger.info("getChangZhanNum ...");
        return changzhanService.getChangzhanNum();
    }

    /**
     * 获取风机数目
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getFengjiNum')")
    @GetMapping("/getFengjiNum")
    public int getFengjiNum(){
        logger.info("getFengjiNum ...");
        return fengjiService.getFengjiNum();
    }

    /**
     * 获取故障总数目
     */
    @DataSource(value = DataSourceType.JYTZ)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getFaultRecordNum')")
    @GetMapping("/getFaultRecordNum")
    public int getFaultRecordNum(){
        logger.info("getFaultRecordNum ...");
        String riqi = null;
        return jytzService.getFaultRecordNum(riqi);
    }
    /**
     * 获取本月故障数目
     */
    @DataSource(value = DataSourceType.JYTZ)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getFaultRecordNum2')")
    @GetMapping("/getFaultRecordNum2")
    public int getFaultRecordNum2(){
        logger.info("getFaultRecordNum2 ...");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        date = calendar.getTime();
        String riqi = sdf.format(date);
        System.out.println(riqi);
        return jytzService.getFaultRecordNum(riqi);
    }

    /**
     * 获取上个月故障数目
     */
    @DataSource(value = DataSourceType.JYTZ)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getFaultRecordAddNum')")
    @GetMapping("/getFaultRecordAddNum")
    public Map<String,Integer> getFaultRecordAddNum(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date1 = new Date();   //上月时间
        Date date = new Date();    //本月时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        date = calendar.getTime();  //本月时间
        String data = sdf.format(date);     //本月时间
        int thisnum =jytzService.getFaultRecordNum(data);   //本月故障次数

        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date1 = calendar.getTime();  //上月时间
        String data1 = sdf.format(date1);     //上月时间
        int shangnum =jytzService.getFaultRecordNum(data1);   //本月故障次数

        Map<String,Integer> result = new HashMap<>();
        int flag=1;  //判断，1为比上月故障多，2为比上月故障少
        int num;
        if (thisnum>shangnum){
            result.put("flag",1);
            num = thisnum - shangnum;
        }else {
            result.put("flag",2);
            num = shangnum - thisnum;
        }
        result.put("param",num);

        return result;
    }

    /**
     * 查询部件故障排名列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getPartsRankList')")
    @GetMapping("/getPartsRankList")
    public TableDataInfo partslist(FaultRecord faultRecord) {

        List<FaultRecord> list = jytzService.getPartsRankList(faultRecord);
        return getDataTable(list);
    }

    /**
     * 查询故障原因排名列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getContentRankList')")
    @GetMapping("/getContentRankList")
    public TableDataInfo contentlist(FaultRecord faultRecord) {

        List<FaultRecord> list = jytzService.getContentRankList(faultRecord);
        return getDataTable(list);
    }

    /**
     * 查询故障线状图
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getBrokenLine')")
    @GetMapping("/getBrokenLine")
    public AjaxResult getBrokenLine(){
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy");
        String year = dateFormat.format(date);
        Map<String,Object> map = jytzService.getAllYearBrokenLine();
        return AjaxResult.success(map);
    }

    /**
     * 增加故障现状图年份
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:yearBrokenLine')")
    @PostMapping("/yearBrokenLine")
    public AjaxResult yearBrokenLine(@RequestBody String[] yearList){
        int count = 0;
        int num = yearList.length;
        count = num;
        Map<String,Object> map = jytzService.getAllYearBrokenLine();
        return AjaxResult.success(count);
    }

    /**
     * 获取饼状图
     * @return
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:yxzk:getPieChart')")
    @GetMapping("/getPieChart")
    public AjaxResult getPieChart(){
        Map<String,Object> map = jytzService.getPieChart();
        return AjaxResult.success(map);
    }
}
